/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package merlion.frm.bpm.entity;

import java.io.Serializable;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;
import merlion.crm.slcm.entity.Customer;
import merlion.crm.sopm.entity.SalesOrder;
import merlion.scm.lcm.entity.DeliveryOrder;

/**
 *
 * @author Kenn & Yuwei
 */
@Entity
public class Invoice implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String invoiceId;
    private Timestamp invoiceDate;
    private Timestamp invoiceDueDate;
    private String invoiceDateStr;
    private String poNumber;
    private Double tax;
    private Double subtotal;
    private Double total;
    private boolean paid;
    private Boolean cashInAdvance;
    @OneToOne(targetEntity = DeliveryOrder.class, cascade = {CascadeType.PERSIST, CascadeType.MERGE}, fetch = FetchType.EAGER, mappedBy="invoice")
    private DeliveryOrder deliveryOrder;
    @OneToOne (targetEntity=CustomerPaymentRecord.class, cascade={CascadeType.ALL}, fetch=FetchType.EAGER)
    private CustomerPaymentRecord customerPaymentRecord;
    @ManyToOne(targetEntity = Customer.class, cascade = {CascadeType.PERSIST, CascadeType.MERGE}, fetch = FetchType.EAGER)
    private Customer customer;
    
    private static final SimpleDateFormat formatter = new SimpleDateFormat("MMMMM dd, yyyy");
    
    public Invoice(){       
        cashInAdvance = Boolean.FALSE;
        invoiceId = "";
        invoiceDate = new Timestamp(new Date().getTime());
        invoiceDueDate = new Timestamp(new Date().getTime());
        invoiceDateStr = convertToDateFormat(invoiceDate);
        paid = Boolean.FALSE;
        deliveryOrder = new DeliveryOrder();
        customerPaymentRecord = new CustomerPaymentRecord();
        customer = new Customer();
    }
    private String convertToDateFormat(Timestamp timeStamp){
        return formatter.format((java.util.Date) timeStamp);
    }
    
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Boolean getCashInAdvance() {
        return cashInAdvance;
    }

    public void setCashInAdvance(Boolean cashInAdvance) {
        this.cashInAdvance = cashInAdvance;
    }
    
    public Customer getCustomer() {
        return customer;
    }

    public void setCustomer(Customer customer) {
        this.customer = customer;
    }

    public CustomerPaymentRecord getCustomerPaymentRecord() {
        return customerPaymentRecord;
    }

    public void setCustomerPaymentRecord(CustomerPaymentRecord customerPaymentRecord) {
        this.customerPaymentRecord = customerPaymentRecord;
    }

    public Timestamp getInvoiceDate() {
        return invoiceDate;
    }

    public void setInvoiceDate(Timestamp invoiceDate) {
        this.invoiceDate = invoiceDate;
    }

    public Timestamp getInvoiceDueDate() {
        return invoiceDueDate;
    }

    public void setInvoiceDueDate(Timestamp invoiceDueDate) {
        this.invoiceDueDate = invoiceDueDate;
    }
    
    public String getInvoiceDateStr() {
        return invoiceDateStr;
    }

    public void setInvoiceDateStr(String invoiceDateStr) {
        this.invoiceDateStr = invoiceDateStr;
    }

    public String getInvoiceId() {
        return invoiceId;
    }

    public void setInvoiceId(String invoiceId) {
        this.invoiceId = invoiceId;
    }


    public DeliveryOrder getDeliveryOrder() {
        return deliveryOrder;
    }

    public void setDeliveryOrder(DeliveryOrder deliveryOrder) {
        this.deliveryOrder = deliveryOrder;
    }
    
    public boolean isPaid() {
        return paid;
    }

    public void setPaid(boolean paid) {
        this.paid = paid;
    }

    public Double getTotal() {
        return total;
    }

    public void setTotal(Double total) {
        this.total = total;
    }

    public String getPoNumber() {
        return poNumber;
    }

    public void setPoNumber(String poNumber) {
        this.poNumber = poNumber;
    }

    public Double getSubtotal() {
        return subtotal;
    }

    public void setSubtotal(Double subtotal) {
        this.subtotal = subtotal;
    }

    public Double getTax() {
        return tax;
    }

    public void setTax(Double tax) {
        this.tax = tax;
    }
    
    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        if (!(object instanceof Invoice)) {
            return false;
        }
        Invoice other = (Invoice) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "merlion.frm.bpm.entity.Invoice[ id=" + id + " ]";
    }
    
}
